package co.fitstart.data.scheduler;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.support.HttpResult;

import co.fitstart.data.handler.LjzClientOrderHandler;
import co.fitstart.data.service.DataService;
import co.fitstart.data.service.OrderService;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Component
public class LjzOrderScheduler {
    private static final Logger logger = LoggerFactory.getLogger(LjzOrderScheduler.class);
    
    private static final Long siteId = 8L;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Site> siteService;
    
    @Autowired
    private OrderService orderServie;
    
    @Autowired
    private LjzClientOrderHandler ljzClientOrderHandler;
    
//    @Scheduled(cron = "0 0 3 * * *")
//    public void scanLjzOrder() {
//        Date now = new Date();
//        Date today = DateUtils.parseDate(DateFormatUtils.format(now, "yyyy-MM-dd"), "yyyy-MM-dd");
//        Date yesterday = DateUtils.addDays(today, -1);
//        
//        logger.info("start running LjzOrderScheduler#scanLjzOrder at {}", now);
//        
//        Site site = siteService.load(Site.class, LjzOrderScheduler.siteId);
//        if(site != null) {
//            List<Order> orders = orderServie.listUnPostLjzOrder(today, yesterday, site);
//            
//            if(orders != null && orders.size() > 0) {
//                logger.info("scaned {} order need to be posted", orders.size());
//                
//                for(Order order : orders) {
//                    
//                    HttpResult httpResult = ljzClientOrderHandler.addData(order);
//                    logger.info("upload order [{}], get result[{}]", order.getId(), JsonUtils.toJsonString(httpResult));
//                    
//                    if(httpResult.isValid()) {
//                        orderServie.uploadedLjz(order);
//                    }
//                }
//            } else {
//                logger.info("scaned 0 order need to be posted", now);
//            }
//        }
//        
//        logger.info("finish running LjzOrderScheduler#scanLjzOrder at {}", new Date());
//    }
}
